{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# **Google**"
      ],
      "metadata": {
        "id": "mL-fAbu5hCLR"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q --upgrade google-cloud-vision"
      ],
      "metadata": {
        "id": "YPkeDbVaiJ_V"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q Pillow"
      ],
      "metadata": {
        "id": "xxmvcRPL1-ve"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import io, os\n",
        "from numpy import random\n",
        "from google.cloud import vision\n",
        "from PIL import Image\n",
        "import pandas as pd"
      ],
      "metadata": {
        "id": "knsbGnrF18u2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r\" \"\n",
        "client = vision.ImageAnnotatorClient()\n",
        "\n",
        "file_name = ' '\n",
        "image_path = os.path.join('.\\Images', file_name)\n",
        "\n",
        "with io.open(image_path, 'rb') as image_file:\n",
        "    content = image_file.read()\n",
        "\n",
        "image = vision.Image(content=content)\n",
        "response = client.object_localization(image=image)\n",
        "localized_object_annotations = response.localized_object_annotations"
      ],
      "metadata": {
        "id": "UgQuaOgR17nf"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "def localize_objects(path):\n",
        "    \"\"\"Localize objects in the local image.\n",
        "\n",
        "    Args:\n",
        "    path: The path to the local file.\n",
        "    \"\"\"\n",
        "    from google.cloud import vision\n",
        "\n",
        "    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = r\" \"\n",
        "    client = vision.ImageAnnotatorClient()\n",
        "\n",
        "    with open(path, \"rb\") as image_file:\n",
        "        content = image_file.read()\n",
        "    image = vision.Image(content=content)\n",
        "\n",
        "    objects = client.object_localization(image=image).localized_object_annotations\n",
        "    bounding_poly = {}\n",
        "    print(f\"Number of objects found: {len(objects)}\")\n",
        "    for object_ in objects:\n",
        "        print(f\"\\n{object_.name} (confidence: {object_.score})\")\n",
        "        print(\"Normalized bounding polygon vertices: \")\n",
        "        d = {}\n",
        "        i = 1\n",
        "        for vertex in object_.bounding_poly.normalized_vertices:\n",
        "            d[f\"normalized_vertices_{i}\"] = {\"x\":vertex.x, \"y\":vertex.y}\n",
        "            print(f\" - ({vertex.x}, {vertex.y})\")\n",
        "            i+=1\n",
        "        print(d)\n",
        "        key = f\"{object_.name}_{object_.score}\"\n",
        "        bounding_poly[key] = d\n",
        "\n",
        "    print(bounding_poly)"
      ],
      "metadata": {
        "id": "RtF0u67uKZoo"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "path = \"/content/image.jpg\"\n",
        "localize_objects(path)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "uLpZyQxGKc-j",
        "outputId": "21b6dacd-19ae-4f9e-e0a0-2d86a412735f"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Number of objects found: 10\n",
            "\n",
            "Bicycle (confidence: 0.9054205417633057)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.16015625, 0.2255859375)\n",
            " - (0.74609375, 0.2255859375)\n",
            " - (0.74609375, 0.73046875)\n",
            " - (0.16015625, 0.73046875)\n",
            "{'normalized_vertices_1': {'x': 0.16015625, 'y': 0.2255859375}, 'normalized_vertices_2': {'x': 0.74609375, 'y': 0.2255859375}, 'normalized_vertices_3': {'x': 0.74609375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.16015625, 'y': 0.73046875}}\n",
            "\n",
            "Tire (confidence: 0.7708812952041626)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.4921875, 0.4140625)\n",
            " - (0.734375, 0.4140625)\n",
            " - (0.734375, 0.73046875)\n",
            " - (0.4921875, 0.73046875)\n",
            "{'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}\n",
            "\n",
            "Tire (confidence: 0.7498802542686462)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.1611328125, 0.408203125)\n",
            " - (0.2890625, 0.408203125)\n",
            " - (0.2890625, 0.65234375)\n",
            " - (0.1611328125, 0.65234375)\n",
            "{'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}\n",
            "\n",
            "Bicycle wheel (confidence: 0.7476080060005188)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.4921875, 0.4140625)\n",
            " - (0.734375, 0.4140625)\n",
            " - (0.734375, 0.73046875)\n",
            " - (0.4921875, 0.73046875)\n",
            "{'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}\n",
            "\n",
            "Car (confidence: 0.6787649989128113)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.60546875, 0.12890625)\n",
            " - (0.921875, 0.12890625)\n",
            " - (0.921875, 0.291015625)\n",
            " - (0.60546875, 0.291015625)\n",
            "{'normalized_vertices_1': {'x': 0.60546875, 'y': 0.12890625}, 'normalized_vertices_2': {'x': 0.921875, 'y': 0.12890625}, 'normalized_vertices_3': {'x': 0.921875, 'y': 0.291015625}, 'normalized_vertices_4': {'x': 0.60546875, 'y': 0.291015625}}\n",
            "\n",
            "Bicycle wheel (confidence: 0.6777045726776123)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.1611328125, 0.408203125)\n",
            " - (0.2890625, 0.408203125)\n",
            " - (0.2890625, 0.65234375)\n",
            " - (0.1611328125, 0.65234375)\n",
            "{'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}\n",
            "\n",
            "Wheel (confidence: 0.6597345471382141)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.1611328125, 0.408203125)\n",
            " - (0.2890625, 0.408203125)\n",
            " - (0.2890625, 0.65234375)\n",
            " - (0.1611328125, 0.65234375)\n",
            "{'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}\n",
            "\n",
            "Wheel (confidence: 0.6327399611473083)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.4921875, 0.4140625)\n",
            " - (0.734375, 0.4140625)\n",
            " - (0.734375, 0.73046875)\n",
            " - (0.4921875, 0.73046875)\n",
            "{'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}\n",
            "\n",
            "Dog (confidence: 0.6011953949928284)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.1708984375, 0.388671875)\n",
            " - (0.40625, 0.388671875)\n",
            " - (0.40625, 0.9375)\n",
            " - (0.1708984375, 0.9375)\n",
            "{'normalized_vertices_1': {'x': 0.1708984375, 'y': 0.388671875}, 'normalized_vertices_2': {'x': 0.40625, 'y': 0.388671875}, 'normalized_vertices_3': {'x': 0.40625, 'y': 0.9375}, 'normalized_vertices_4': {'x': 0.1708984375, 'y': 0.9375}}\n",
            "\n",
            "Wheel (confidence: 0.5077753067016602)\n",
            "Normalized bounding polygon vertices: \n",
            " - (0.1708984375, 0.388671875)\n",
            " - (0.40625, 0.388671875)\n",
            " - (0.40625, 0.9375)\n",
            " - (0.1708984375, 0.9375)\n",
            "{'normalized_vertices_1': {'x': 0.1708984375, 'y': 0.388671875}, 'normalized_vertices_2': {'x': 0.40625, 'y': 0.388671875}, 'normalized_vertices_3': {'x': 0.40625, 'y': 0.9375}, 'normalized_vertices_4': {'x': 0.1708984375, 'y': 0.9375}}\n",
            "{'Bicycle_0.9054205417633057': {'normalized_vertices_1': {'x': 0.16015625, 'y': 0.2255859375}, 'normalized_vertices_2': {'x': 0.74609375, 'y': 0.2255859375}, 'normalized_vertices_3': {'x': 0.74609375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.16015625, 'y': 0.73046875}}, 'Tire_0.7708812952041626': {'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}, 'Tire_0.7498802542686462': {'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}, 'Bicycle wheel_0.7476080060005188': {'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}, 'Car_0.6787649989128113': {'normalized_vertices_1': {'x': 0.60546875, 'y': 0.12890625}, 'normalized_vertices_2': {'x': 0.921875, 'y': 0.12890625}, 'normalized_vertices_3': {'x': 0.921875, 'y': 0.291015625}, 'normalized_vertices_4': {'x': 0.60546875, 'y': 0.291015625}}, 'Bicycle wheel_0.6777045726776123': {'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}, 'Wheel_0.6597345471382141': {'normalized_vertices_1': {'x': 0.1611328125, 'y': 0.408203125}, 'normalized_vertices_2': {'x': 0.2890625, 'y': 0.408203125}, 'normalized_vertices_3': {'x': 0.2890625, 'y': 0.65234375}, 'normalized_vertices_4': {'x': 0.1611328125, 'y': 0.65234375}}, 'Wheel_0.6327399611473083': {'normalized_vertices_1': {'x': 0.4921875, 'y': 0.4140625}, 'normalized_vertices_2': {'x': 0.734375, 'y': 0.4140625}, 'normalized_vertices_3': {'x': 0.734375, 'y': 0.73046875}, 'normalized_vertices_4': {'x': 0.4921875, 'y': 0.73046875}}, 'Dog_0.6011953949928284': {'normalized_vertices_1': {'x': 0.1708984375, 'y': 0.388671875}, 'normalized_vertices_2': {'x': 0.40625, 'y': 0.388671875}, 'normalized_vertices_3': {'x': 0.40625, 'y': 0.9375}, 'normalized_vertices_4': {'x': 0.1708984375, 'y': 0.9375}}, 'Wheel_0.5077753067016602': {'normalized_vertices_1': {'x': 0.1708984375, 'y': 0.388671875}, 'normalized_vertices_2': {'x': 0.40625, 'y': 0.388671875}, 'normalized_vertices_3': {'x': 0.40625, 'y': 0.9375}, 'normalized_vertices_4': {'x': 0.1708984375, 'y': 0.9375}}}\n"
          ]
        }
      ]
    }
  ]
}